function [p,c,d,imagesPCA] = lowDimPCA(images,percent)
    % images are the images to reduce
    % d is the number of features to keep.
    
    %%%%%%%%%%%%%%%
    % Perform PCA %
    %%%%%%%%%%%%%%%
    [p,c] = pca(images, size(images,2)-1);
    scatterM = (size(images,1) - 1) * cov(images);
    [~, eigVals] = eigs(scatterM,[],size(scatterM,1)-1);

    % find the number of dimensions that retains at least 90% of the variance
    denom = 0.0;
    for i = 1:size(eigVals,1)
        denom = denom + eigVals(i,i);
    end
    varRetained = zeros(size(eigVals,1),1);
    for j = 1:size(eigVals,1)
        eigSum = 0.0;
        for k = 1:j
            eigSum = eigSum + eigVals(k,k);
        end
        varRetained(j,1) = eigSum/denom;
    end
    
    d = find(varRetained>percent,1,'first');

    imagesPCA = (images - repmat(c, size(images,1), 1)) * p(:,1:d);

end